'''
Created on Apr 7, 2009

@author: Administrator
'''
from django.contrib import auth
from django.forms import forms
from django.contrib.auth.decorators import login_required
from django.contrib.auth.models import User
from com.ericyang.groupshopping.user.models import UserDetail
from com.ericyang.groupshopping.user.forms import UserDetailForm, ChangePasswordForm, UserEditForm
from django.contrib.auth.views import login, logout
from django.contrib.auth import SESSION_KEY
from django.utils.translation import ugettext_lazy as _
from com.ericyang.common.web import redirect, page404, response, clear_response
from com.ericyang.common.web.middleware import threadlocals


RELATIVE_TEMPLATE_DIR = 'user/'
def template(template_name):
    return RELATIVE_TEMPLATE_DIR + template_name;

# user register
def register(request):    
    if request.method == 'POST':        
        form = UserDetailForm(request.POST)
        if form.is_valid():
            try:
                new_user = form.save()
                return redirect("/login/")
            except forms.ValidationError:
                return response("user/user_register.html", { 'form' : form })
    else:
        form = UserDetailForm()

    return response("user/user_register.html", { 'form' : form })

# user index
def index_user(request, username=None):
    if not username:
        username = threadlocals.get_current_user().username;
    try:
        userdetail = UserDetail.objects.get(username__exact=username)
    except UserDetail.DoesNotExist:
        return redirect("/login/");
    else:
        return clear_response(template('user_detail.html'), {'user': userdetail})
    
# view user profile<br>
# if no username given,then view current user's,<br>
# otherwise view the profile of the user given.
@login_required
def view_user(request, username=None):
    if not username:
        username = threadlocals.get_current_user().username;
    
    try:
        userdetail = UserDetail.objects.get(username__exact=username)
    except UserDetail.DoesNotExist:
        return response(template('user_notexist.html'), {'username': username})
    else:
        return clear_response(template('user_detail.html'), {'user': userdetail})

#edit user's profile
@login_required
def edit_user(request):
    user = threadlocals.get_current_user()
    username = user.username
    try:
        userdetail = UserDetail.objects.get(username__exact=username)
    except UserDetail.DoesNotExist:
        return response(template('user_notexist.html'), {'username': username})
    else:
        if request.method == 'POST':
            form = UserEditForm(userdetail, request.POST)
            if form.is_valid():
                user = form.save()
                return index_user(request, username)
        else:
            form = UserEditForm(userdetail, {"nickname":userdetail.nickname, "email":userdetail.email})
        return clear_response(template('user_edit.html'), {'form':form, 'user': userdetail})

# change user's password 
@login_required
def changepw(request):
    user = threadlocals.get_current_user()
    username = user.username
    try:
        userdetail = UserDetail.objects.get(username__exact=username)
    except UserDetail.DoesNotExist:
        return response(template('user_notexist.html'), {'username': username})
    else:
        if request.method == 'POST':        
            form = ChangePasswordForm(userdetail, request.POST)
            if form.is_valid():
                user = form.save()
                return index_user(request, username)
        else:
            form = ChangePasswordForm(userdetail)
    
        return clear_response("user/user_changepw.html", {'form':form, 'user':userdetail})

    
    